package com.kurumi.medical.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kurumi.medical.service.ExportDataService;
import com.kurumi.medical.service.HospNoteBasicJsonService;
import com.kurumi.medical.service.HospitalVisitService;
import com.kurumi.medical.service.impl.HospNoteBasicJsonServiceImpl;
import com.kurumi.medical.util.MysqlDataTable;
import com.kurumi.medical.util.OracleDataTable;
import com.kurumi.medical.util.Result;

@Controller
@RequestMapping("/export")
public class ExportDataController {
	
	
	
	@Autowired
	private ExportDataService exportDataService;
	
	@Autowired
	private HospitalVisitService hospitalVisitService;
	
	@Autowired
	private HospNoteBasicJsonService hospNoteBasicJsonService;
	
	@RequestMapping("/index")
	public String index(){
		return "index";
	}
	
	@RequestMapping("/exportMedicalHospitalRecord")
	@ResponseBody
	public String exportMedicalHospitalRecord(Model model){
		List<Map<String, Object>> datas = hospitalVisitService.getDatasByOracle();
		hospitalVisitService.insertHospitalVisitRecords(datas);
		return null;
	}
	
	@RequestMapping("/exportTable")
	public String exportTable(Model model){
		
		List<Result> list = new ArrayList<>();
		
		List<Map<String,Object>> otherList = new ArrayList<>();
		//身份信息
		List<Map<String, Object>> identitys = exportDataService.getIdentityByOracle();
		
		if(!identitys.isEmpty()){
			Map<String,Object> dataMap = new HashMap<>();
			dataMap.put("tableName",MysqlDataTable.IDENTITY);
			dataMap.put("datas", identitys);
			dataMap.put("oracleTableName", OracleDataTable.IDENTITY);
			otherList.add(dataMap);
		}else{
			list.add(new Result(true,"无数据可导入",OracleDataTable.IDENTITY));
		}
		/*
		List<Map<String, Object>> clinics = exportDataService.getClinicAttrByOracle();
		if(!clinics.isEmpty()){
			Map<String,Object> dataMap = new HashMap<>();
			dataMap.put("tableName",MysqlDataTable.OFFICECLINICATTR);
			dataMap.put("datas", clinics);
			dataMap.put("oracleTableName", OracleDataTable.DEPTCLINICATTR);
			otherList.add(dataMap);
		}else{
			list.add(new Result(false,"无数据可导入",OracleDataTable.DEPTCLINICATTR));
		}
		
		List<Map<String, Object>> internalOuts = exportDataService.getInternalOutByOracle();
		
		if(!internalOuts.isEmpty()){
			Map<String,Object> dataMap = new HashMap<>();
			dataMap.put("tableName",MysqlDataTable.OFFICEINTERNALOUTATTR);
			dataMap.put("datas", internalOuts);
			dataMap.put("oracleTableName", OracleDataTable.DEPTISATTR);
			otherList.add(dataMap);
		}else{
			list.add(new Result(false,"无数据可导入",OracleDataTable.DEPTISATTR));
		}
		
		List<Map<String, Object>> outPAttrs = exportDataService.getOutpByOracle();
		
		if(!outPAttrs.isEmpty()){
			Map<String,Object> dataMap = new HashMap<>();
			dataMap.put("tableName",MysqlDataTable.OFFICEOUTPATTR);
			dataMap.put("datas", outPAttrs);
			dataMap.put("oracleTableName", OracleDataTable.DEPTOIATTR);
			otherList.add(dataMap);
		}else{
			list.add(new Result(false,"无数据可导入",OracleDataTable.DEPTOIATTR));
		}
		
		

		List<Map<String,Object>> listDatas = new ArrayList<>();
		
		//性别
		listDatas.addAll(exportDataByTableName(list,OracleDataTable.SEX,MysqlDataTable.SEX));
		
		//婚姻
		listDatas.addAll(exportDataByTableName(list,OracleDataTable.MARRIAGE,MysqlDataTable.MARRIAGE));
		
		//民族
		listDatas.addAll(exportDataByTableName(list,OracleDataTable.NATION,MysqlDataTable.NATION));
		
		//血型
		listDatas.addAll(exportDataByTableName(list,OracleDataTable.BLOODTYPE,MysqlDataTable.BLOODTYPE));
		
		//职业分类
		listDatas.addAll(exportDataByTableName(list,OracleDataTable.OCCUPATION,MysqlDataTable.PROFESSION));
		
		//入院方式
		listDatas.addAll(exportDataByTableName(list,OracleDataTable.PATIENTCLASS,MysqlDataTable.INHOSPTYPE));
		
		//出院方式
		listDatas.addAll(exportDataByTableName(list,OracleDataTable.DISCHARGEMODE,MysqlDataTable.OUTHOSTYPE));
		
		//社会关系
		listDatas.addAll(exportDataByTableName(list,OracleDataTable.RELATIONSHIP,MysqlDataTable.RELATIVERELATION));
		
		//入院时病情
		List<Map<String, Object>> inHosStates = exportDataService.getInHospStatusByOracle();

		if(!inHosStates.isEmpty()){
			Map<String,Object> dataMap = new HashMap<>();
			dataMap.put("tableName",MysqlDataTable.INHOSSTATE);
			dataMap.put("datas", inHosStates);
			dataMap.put("oracleTableName", OracleDataTable.ILLNESSSTATE);
			otherList.add(dataMap);
		}else{
			list.add(new Result(false,"无数据可导入",OracleDataTable.ILLNESSSTATE));
		}
		
		//技术职务
		List<Map<String, Object>> titiles = exportDataService.getTitileByOracle();

		if(!titiles.isEmpty()){
			Map<String,Object> dataMap = new HashMap<>();
			dataMap.put("tableName",MysqlDataTable.MEDICAL_PROFESSIONAL_TITLE);
			dataMap.put("datas", titiles);
			dataMap.put("oracleTableName", OracleDataTable.TITLE);
			otherList.add(dataMap);
		}else{
			list.add(new Result(false,"无数据可导入",OracleDataTable.TITLE));
		}
		//工作职业
		List<Map<String, Object>> jobClass = exportDataService.getJobClassByOracle();

		if(!jobClass.isEmpty()){
			Map<String,Object> dataMap = new HashMap<>();
			dataMap.put("tableName",MysqlDataTable.MEDICAL_PROFESSION);
			dataMap.put("datas", jobClass);
			dataMap.put("oracleTableName", OracleDataTable.JOB_CLASS);
			otherList.add(dataMap);
		}else{
			list.add(new Result(false,"无数据可导入",OracleDataTable.JOB_CLASS));
		}
		
		//国家
		listDatas.addAll(exportDataByTableName(list,OracleDataTable.COUNTRY,MysqlDataTable.NATIONALITY));
		
		//地区
		listDatas.addAll(exportDataByTableName(list,OracleDataTable.AREA,MysqlDataTable.AREA));
		
		//科室
		List<Map<String, Object>> offices = exportDataService.getOfficeByOracle();
		
		if(!offices.isEmpty()){
			Map<String,Object> dataMap = new HashMap<>();
			dataMap.put("tableName",MysqlDataTable.MEDICALOFFICE);
			dataMap.put("datas", offices);
			dataMap.put("oracleTableName", OracleDataTable.DEPT);
			otherList.add(dataMap);
		}else{
			list.add(new Result(false,"无数据可导入",OracleDataTable.DEPT));
		}
		//诊断类型
		listDatas.addAll(exportDataByTableName(list,OracleDataTable.DIAGNOSETYPE,MysqlDataTable.DIAGNOSETYPE));
		//麻醉方式
		listDatas.addAll(exportDataByTableName(list,OracleDataTable.ANAESTHESIA,MysqlDataTable.ANAESTHESIAWAY));
		
		
		//切口愈合
		listDatas.addAll(exportDataByTableName(list,OracleDataTable.WOUNDHEALING,MysqlDataTable.WOUNDHEALINGTYPE));
		
		//手术等级
		listDatas.addAll(exportDataByTableName(list,OracleDataTable.OPERATIONLEVEL,MysqlDataTable.OPERATIONLEVEL));
		
		//治疗结果
		listDatas.addAll(exportDataByTableName(list,OracleDataTable.TREATINGRESULT,MysqlDataTable.TREATINGRESULT));
		
		//切口等级
		listDatas.addAll(exportDataByTableName(list,OracleDataTable.WOUNDLEVEL,MysqlDataTable.WOUNDLEVEL));
		
		//费用类别
		List<Map<String, Object>> chargeTypes = exportDataService.getChargeTypeByOracle();
		
		if(!chargeTypes.isEmpty()){
			Map<String,Object> dataMap = new HashMap<>();
			dataMap.put("tableName",MysqlDataTable.PAYMENT_TYPE);
			dataMap.put("datas", chargeTypes);
			dataMap.put("oracleTableName", OracleDataTable.CHARGE_TYPE);
			otherList.add(dataMap);
		}else{
			list.add(new Result(false,"无数据可导入",OracleDataTable.CHARGE_TYPE));
		}
		//诊断符合
		listDatas.addAll(exportDataByTableName(list,OracleDataTable.DIAG_CORR,MysqlDataTable.DIAGNOSTIC_COINCIDENCE_TYPE));
		
		
		//国际疾病
		List<Map<String, Object>> diagnosiss = exportDataService.getDiagnosisByOracle();
		
		if(!diagnosiss.isEmpty()){
			Map<String,Object> dataMap = new HashMap<>();
			dataMap.put("tableName",MysqlDataTable.DISEASE);
			dataMap.put("datas", diagnosiss);
			dataMap.put("oracleTableName", OracleDataTable.DIAGNOSIS);
			otherList.add(dataMap);
		}else{
			list.add(new Result(false,"无数据可导入",OracleDataTable.DIAGNOSIS));
		}
		//手术
		List<Map<String, Object>> operations = exportDataService.getOperationsByOracle();
		
		if(!operations.isEmpty()){
			Map<String,Object> dataMap = new HashMap<>();
			dataMap.put("tableName",MysqlDataTable.OPERATION);
			dataMap.put("datas", operations);
			dataMap.put("oracleTableName", OracleDataTable.OPERATION);
			otherList.add(dataMap);
		}else{
			list.add(new Result(false,"无数据可导入",OracleDataTable.OPERATION));
		}
		
		List<Map<String, Object>> staffs = exportDataService.getStaffsByOracle();
		if(!staffs.isEmpty()){
			Map<String,Object> dataMap = new HashMap<>();
			dataMap.put("tableName",MysqlDataTable.MEDICAL_WORKER);
			dataMap.put("datas", staffs);
			dataMap.put("oracleTableName", OracleDataTable.STAFF);
			otherList.add(dataMap);
		}else{
			list.add(new Result(false,"无数据可导入",OracleDataTable.STAFF));
		}
		list.addAll(exportDataService.inserDataTable(listDatas,otherList));
		
		*/
		List<Map<String, Object>> medicalRecords = exportDataService.getOracleMedicalHospitalRecord();
		exportDataService.insertMysqlMedicalHospitalRecord(medicalRecords);
		
		model.addAttribute("results", list);
		return "export/exportbasic";
	}
	
	public List<Map<String,Object>> exportDataByTableName(List<Result> results,String oracleTableName,String insertTableName){
		List<Map<String, Object>> listData = new ArrayList<>();
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("prefix", oracleTableName.substring(0,oracleTableName.lastIndexOf("_DICT")));
		map.put("tableName", oracleTableName);
		List<Map<String, Object>> datas = exportDataService.getDataByOracle(map);
		
		if(!datas.isEmpty()){
			Map<String,Object> dataMap = new HashMap<>();
			dataMap.put("tableName",insertTableName);
			dataMap.put("datas", datas);
			dataMap.put("oracleTableName", oracleTableName);
			listData.add(dataMap);
		}else{
			results.add(new Result(false,"无数据可导入",oracleTableName));
		}
		
		return listData;
	}
}